package com.tbit.main.service.impl;

import com.tbit.main.constant.DaoParamConstant;
import com.tbit.main.dao.log.BmsAbnormalDao;
import com.tbit.main.pojo.BmsAbnormal;
import com.tbit.main.pojo.BmsAbnormalGroup;
import com.tbit.main.service.BmsAbnormalService;
import com.tbit.main.service.BmsBatteryService;
import com.tbit.main.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LMY
 * @create 2022-07-18 14:17
 */
@Service("/bmsAbnormalService")
public class BmsAbnormalServiceImpl implements BmsAbnormalService {

    @Autowired
    private BmsAbnormalDao bmsAbnormalDao;
    @Autowired
    private BmsBatteryService bmsBatteryService;

    @Override
    public List<BmsAbnormal> getAbnormal(Integer adAccountId, Integer abnormalType, Integer handle, String batteryNO, String startTime, String endTime) {
        Map<String, Object> map = new HashMap<>();
        map.put("adAccountId", adAccountId);
        map.put("abnormalType", abnormalType);
        map.put("handle", handle);
        map.put("batteryNO", batteryNO);
        map.put(DaoParamConstant.startTime, startTime);
        map.put(DaoParamConstant.endTime, endTime);
        return bmsAbnormalDao.getAbnormal(map);
    }

    @Override
    public BmsAbnormalGroup abnormalGroup(Integer adAccountId, Integer abnormalType, String batteryNO, String startTime, String endTime) {
        Integer abnormal = abnormal(adAccountId, abnormalType, batteryNO, startTime, endTime);
        Integer abnormalLog = abnormalLog(adAccountId, abnormalType, batteryNO, startTime, endTime);
        List<String> abnormalBatteryNOs = abnormalBatteryNO(adAccountId, abnormalType, batteryNO, startTime, endTime);
        BmsAbnormalGroup bmsAbnormalGroup = new BmsAbnormalGroup();
        bmsAbnormalGroup.setAbnormalBms(abnormal);
        bmsAbnormalGroup.setAbnormalBmsLog(abnormalLog);
        bmsAbnormalGroup.setBatteryNOAbnormal(abnormalBatteryNOs);
        return bmsAbnormalGroup;
    }

    @Override
    public void handle(Integer adAccountId, Integer abnormalType, String batteryNO, String startTime, String endTime) {

        List<String> abnormalBatteryNOs = abnormalBatteryNO(adAccountId, abnormalType, batteryNO, startTime, endTime);
        if (abnormalBatteryNOs != null && abnormalBatteryNOs.size() > 0) {
            bmsAbnormalDao.handle(StringUtil.getStringListString(abnormalBatteryNOs));
            bmsBatteryService.updateHandle(StringUtil.getStringListString(abnormalBatteryNOs));
        }
    }

    @Override
    public Integer abnormal(Integer adAccountId, Integer abnormalType, String batteryNO, String startTime, String endTime) {
        Map<String, Object> map = new HashMap<>();
        map.put("adAccountId", adAccountId);
        map.put("abnormalType", abnormalType);
        map.put("batteryNO", batteryNO);
        map.put(DaoParamConstant.startTime, startTime);
        map.put(DaoParamConstant.endTime, endTime);
        return bmsAbnormalDao.abnormal(map);
    }

    @Override
    public List<String> abnormalBatteryNO(Integer adAccountId, Integer abnormalType, String batteryNO, String startTime, String endTime) {
        Map<String, Object> map = new HashMap<>();
        map.put("adAccountId", adAccountId);
        map.put("abnormalType", abnormalType);
        map.put("batteryNO", batteryNO);
        map.put(DaoParamConstant.startTime, startTime);
        map.put(DaoParamConstant.endTime, endTime);
        return bmsAbnormalDao.abnormalBatteryNO(map);
    }

    @Override
    public Integer abnormalLog(Integer adAccountId, Integer abnormalType, String batteryNO, String startTime, String endTime) {
        Map<String, Object> map = new HashMap<>();
        map.put("adAccountId", adAccountId);
        map.put("abnormalType", abnormalType);
        map.put("batteryNO", batteryNO);
        map.put(DaoParamConstant.startTime, startTime);
        map.put(DaoParamConstant.endTime, endTime);
        return bmsAbnormalDao.abnormalLog(map);
    }

    @Override
    public void delete(String batteryNO) {
        bmsAbnormalDao.delete(batteryNO);
    }
}
